package org.paraj.mapreduce.executors;

import java.util.Collection;
import java.util.ArrayList;

import org.paraj.mapreduce.Task;
import org.paraj.mapreduce.SimpleTask;
import org.paraj.mapreduce.ComplexTask;

/**
 * Created by IntelliJ IDEA.
 * User: piotrga
 * Date: Nov 11, 2008
 * Time: 7:52:36 PM
 * To change this template use File | Settings | File Templates.
 */
public class SingleThreadComplexTaskExecutor implements MapReduceExecutor{

    public <R> R run(Task<R> task) {
        if (task instanceof SimpleTask) return ((SimpleTask<R>) task).execute();

        Collection res = new ArrayList();
        for(Object t : ((ComplexTask) task).generateSubtasks()){
                res.add(run((Task)t));
         }
        return ((ComplexTask<R, ?>) task).getResultAggregator().aggregate(res);
    }
}
